<template>
  <div class="container">
    <div class="app-container">
      <el-button class="btn-add" size="mini" type="primary" @click="addPermission(0, 1)">添加权限</el-button>
      <el-table :data="list" row-key="id" default-expand-all>
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="code" label="标识" />
        <el-table-column prop="description" label="描述" />
        <el-table-column label="操作">
          <template #default="{row}">
            <el-button v-if="row.type === 1" type="text" size="mini" @click="addPermission(row.id, 2)">添加</el-button>
            <el-button type="text" size="mini" @click="editPermission(row.id)">编辑</el-button>
            <el-button type="text" size="mini" @click="delPermission(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 添加/编辑权限的弹出层 -->
      <!-- 放置一个弹层 用来编辑新增节点 -->
      <el-dialog :title="`${showText}权限点`" :visible="showDialog" @close="btnCancel">
        <!-- 表单 -->
        <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:90%" />
          </el-form-item>
          <el-form-item label="权限描述">
            <el-input v-model="formData.description" style="width:90%" />
          </el-form-item>
          <el-form-item label="开启">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <el-row slot="footer" type="flex" justify="center">
          <el-col :span="6">
            <el-button size="small" type="primary" @click="btnOK">确定</el-button>
            <el-button size="small" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { reqGetPermissionList, reqAddPermission, reqUpdatePermission, reqGetPermissionDetail, reqDelPermission } from '@/api/permission'
import { transListToTreeData } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 默认关闭
      },
      currentNode: {},
      rules: {
        name: [
          { required: true, message: '权限名称不能为空', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              // 1.1 获取所有的部门数组
              let result = await reqGetPermissionList()

              // 1.3 判断编辑状态
              if (this.formData.id) {
                // 从上面数组中把自己排除掉
                result = result.filter(item => item.id !== this.formData.id)
              }

              // 1.2 判断输入的部门是否在部门数组中
              if (result.some(item => item.name === value)) {
                callback(new Error('已经存在相同的权限名称'))
              } else {
                callback()
              }
            }
          }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' },
          {
            validator: async(rule, value, callback) => {
              // 1.1 获取所有的部门数组
              let result = await reqGetPermissionList()

              // 1.3 判断编辑状态
              if (this.formData.id) {
                // 从上面数组中把自己排除掉
                result = result.filter(item => item.id !== this.formData.id)
              }

              // 1.2 判断输入的部门是否在部门数组中
              if (result.some(item => item.code === value)) {
                callback(new Error('已经存在相同的权限标识'))
              } else {
                callback()
              }
            }
          }
        ]
      },
      showDialog: false
    }
  },
  computed: {
    showText() {
      return this.formData.id ? '编辑' : '新增'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 1. 获取权限列表
    async getPermissionList() {
      this.list = transListToTreeData(await reqGetPermissionList(), 0)
    },
    // 2. 添加权限
    // pid: 父级权限的id
    // type: 1-一级页面访问权限  2-二级按钮操作权限
    addPermission(pid, type) {
      this.formData.pid = pid
      this.formData.type = type
      this.showDialog = true
      this.currentNode.pid = pid
    },
    async btnOK() {
      await this.$refs.perForm.validate()
      let title = '新增'
      if (this.formData.id) { // 编辑
        await reqUpdatePermission(this.formData)
        title = '编辑'
      } else { // 添加
        await reqAddPermission(this.formData)
      }
      //  提示消息
      this.$message.success(`${title}成功`)
      this.getPermissionList()
      this.showDialog = false
    },
    btnCancel() {
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: '', // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: '0' // 默认关闭
      }
      this.$refs.perForm.resetFields()
      this.showDialog = false
    },
    // 3. 编辑
    async editPermission(id) {
      // 根据获取id获取详情
      const res = await reqGetPermissionDetail(id)
      this.formData = res
      this.showDialog = true
      this.currentNode = { ...res }
    },
    // 4. 删除
    async delPermission(id) {
      try {
        await this.$confirm('确定要删除该数据吗')
        await reqDelPermission(id)
        this.getPermissionList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.btn-add{
  margin: 10px;
}
</style>
